package com.coderg.gmall.item.service.impl;

import com.coderg.gmall.item.service.ItemService;
import com.coderg.gmall.model.product.BaseCategoryView;
import com.coderg.gmall.model.product.SkuInfo;
import com.coderg.gmall.model.product.SpuSaleAttr;
import com.coderg.gmall.product.ItemProductFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Description
 * @Author CoderG
 * @Date 2025/5/22 19:48
 **/
@Service
public class ItemServiceImpl implements ItemService {
    @Resource
    ItemProductFeign itemFeign;
    @Autowired
    ThreadPoolExecutor threadPoolExecutor;

    /**
     * sd
     *
     * @param skuId
     * @return
     */
    @Override
    public Map<String, Object> getSkuInfo(Long skuId) {
        ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
        //查询sku

        CompletableFuture<SkuInfo> future = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = itemFeign.getSkuInfo(skuId);
            if (skuInfo == null && skuInfo.getId() == null) {
                return null;
            }
            //组装sku图片
            map.put("skuImage", skuInfo.getSkuImageList());
            //清除图片
            skuInfo.setSkuImageList(null);
            //组装sku
            map.put("skuInfo", skuInfo);

            return skuInfo;
        }, threadPoolExecutor);
        //查并组装分类信息
        CompletableFuture<Void> future1 = future.thenAcceptAsync(skuInfo -> {
            if (skuInfo == null && skuInfo.getId() == null) {
                return;
            }
            BaseCategoryView baseCategoryView = itemFeign.getBaseCategory(skuInfo.getCategory3Id());
            map.put("baseCategoryView", baseCategoryView);
        }, threadPoolExecutor);

        //单独查询一次价格
        CompletableFuture<Void> future2 = future.thenAcceptAsync(skuInfo -> {
            if (skuInfo == null && skuInfo.getId() == null) {
                return;
            }
            BigDecimal bigDecimal = itemFeign.getSkuPrice(skuId);
            map.put("price", bigDecimal);
        }, threadPoolExecutor);
        //查询spu的销售属性和值，并标识当前sku选中的销售属性值
        CompletableFuture<Void> future3 = future.thenAcceptAsync(skuInfo -> {
            if (skuInfo == null && skuInfo.getId() == null) {
                return;
            }
            List<SpuSaleAttr> spuAttrValueBy = itemFeign.getSpuAttrValueBy(skuInfo.getSpuId(), skuId);
            map.put("spuAttrValueBy", spuAttrValueBy);
        }, threadPoolExecutor);


        //获取销售属性 和具体商品sku值的对应关系 键值对关系返回  sku-->sale_attr_value_id
        CompletableFuture<Void> future4 = future.thenAcceptAsync(skuInfo -> {
            if (skuInfo == null && skuInfo.getId() == null) {
                return;
            }
            Map skuAndSale = itemFeign.getSkuAndSaleBySpuId(skuInfo.getSpuId());
            map.put("skuAndSale", skuAndSale);
        }, threadPoolExecutor);
        CompletableFuture.allOf(future1, future2, future3, future4).join();

        //异步更新热度值 TODO  消息中间键

        return map;


/*         HashMap<String, Object> map = new HashMap<>();

        //查询sku
        SkuInfo skuInfo = itemFeign.getSkuInfo(skuId);

        //组装sku图片
        map.put("skuImage",skuInfo.getSkuImageList());
        //组装sku
        skuInfo.setSkuImageList(null);
        map.put("skuInfo",skuInfo);
        //组装分类信息
        BaseCategoryView baseCategoryView = itemFeign.getBaseCategory(skuInfo.getCategory3Id());
        map.put("baseCategoryView",baseCategoryView);
        //单独查询一次价格
        BigDecimal bigDecimal = itemFeign.getSkuPrice(skuId);
        map.put("price",bigDecimal);
        //查询spu的销售属性和值，并标识当前sku选中的销售属性值
        List<SpuSaleAttr> spuAttrValueBy = itemFeign.getSpuAttrValueBy(skuInfo.getSpuId(), skuId);
        map.put("spuAttrValueBy",spuAttrValueBy);
        //获取销售属性 和具体商品sku值的对应关系 键值对关系返回  sku-->sale_attr_value_id
        Map skuAndSale = itemFeign.getSkuAndSaleBySpuId(skuInfo.getSpuId());
        map.put("skuAndSale",skuAndSale);
        return map; */
    }
}
